package mo.leetcode_top;

public class L695 {

    public int maxAreaOfIsland(int[][] grid) {

        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                ans = Math.max(ans, dfs(grid, i, j));
            }
        }
        return ans;
    }


    private int dfs(int[][] grid, int curI, int curJ) {
        if (curI < 0 || curJ < 0 || curI == grid.length || curJ == grid[0].length || grid[curI][curJ] == 0) {
            return 0;
        }
        grid[curI][curJ] = 0;
        int[] di = {1, -1, 0, 0};
        int[] dj = {0, 0, 1, -1};
        int ans = 1;
        for (int i = 0; i < di.length; i++) {
            int nextI = curI + di[i];
            int nextJ = curJ + dj[i];
            ans += dfs(grid, nextI, nextJ);
        }
        return ans;
    }

}
